import { SendTransactionRequest, TonConnect } from '@tonconnect/sdk';
import { accountParse } from "src/utils/accountUtils"
import * as inspector from "inspector";
import { beginCell } from '@ton/ton'

import TonWeb from "tonweb";

/**
 * TON转账
 * @param tonConnectUI UI对象
 * @param amount 金额 TON/个
 * @param orderId 订单ID
 * @returns {Promise<void>}
 *
 */

export const tonTransfer = async (tonConnectUI:any,amount:number,orderId:string) => {
        let hash = accountParse(import.meta.env.VITE_TON_TO_FROM_ADDRESS)
        let num=1000000 * 1000 * amount //
        const body = beginCell()
          .storeUint(0, 32) // 写入32个零位以表示后面将跟随文本评论
          .storeStringTail(orderId) // 写下我们的文本评论
          .endCell();
        const defaultTx: SendTransactionRequest = {
            validUntil: Math.round(Date.now() / 1000) + 1000,
            //TON汇率  1000000 = 0.001 TON
            //手续费按照 0.001 算
            messages: [
                {
                    address: hash,
                    amount: num+"",
                    payload: body.toBoc().toString("base64") // body中带有评论的载荷
                    // payload:'123456789',
                }
            ],
        };
        console.log("BOC::",body.toBoc().toString("base64"))
        console.log("BAA::",defaultTx)
        return  tonConnectUI.sendTransaction(defaultTx, {
            modals: 'all',
            notifications: 'all'
        });

}